package com.looker.job.zhang.alex.spider.mq;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

import com.rabbitmq.client.Channel;

public class LoadContent51jobConsumer implements ChannelAwareMessageListener {

	@Override
	public void onMessage(Message message, Channel channel) throws Exception {
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		String url = new String(message.getBody());
		System.out.println(url);
		HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr/t_job");
		SolrQuery query = new SolrQuery();
		// 3.设置查询条件
		String solrQueryUrl = url.replaceAll("\\:", "\\\\:").replaceAll("\\-", "\\\\-").replaceAll("\\?", "\\\\?");
		System.out.println(solrQueryUrl);
		query.set("q", "url:" + solrQueryUrl);// 设置查询关键字
		query.setStart(1);
		query.setRows(1);// 分页条件
		// 4、执行查询
		QueryResponse queryResponse = solrServer.query(query);
		// 5.获取文档列表
		SolrDocumentList documentList = queryResponse.getResults();
		// 获取总记录数
		long numFound = documentList.getNumFound();
		if (numFound > 0) {
			System.err.println("found");
			return;
		}
		Document doc = null;
		int times = 1;
		while (doc == null && times < 3) {
			try {
				doc = Jsoup.connect(url).get();
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("continue:" + times);
				++times;
			}
		}
		if (doc == null) {
			return;
		}
		String jobName = doc.select(".cn").get(0).select("h1").text();
		System.out.println("jobName:" + jobName);
		String location = doc.select(".lname").get(0).text();
		System.out.println("location:" + location);
		String salary = doc.select(".lname").get(0).nextElementSibling().text();
		System.out.println("salary:" + salary);
		// 1.salary解析
		BigDecimal salaryMin = new BigDecimal("0.0");
		BigDecimal salaryMax = new BigDecimal("99999999999999999999999999");
		String salaryUnit = "";
		if (salary != null && !"面议".equals(salary)) {
			if (salary.indexOf("万") > -1) {
				salaryUnit = salary.substring(salary.indexOf("万") + 1);
				if (salary.indexOf("-") > -1) {
					salaryMin = new BigDecimal(salary.substring(0, salary.indexOf("-")))
							.multiply(new BigDecimal("10000"));
					salaryMax = new BigDecimal(salary.substring(salary.indexOf("-") + 1, salary.indexOf("万")))
							.multiply(new BigDecimal("10000"));
				} else {
					salaryMin = new BigDecimal(salary.substring(0, salary.indexOf("万")))
							.multiply(new BigDecimal("10000"));
				}
			} else if (salary.indexOf("千") > -1) {
				salaryUnit = salary.substring(salary.indexOf("千") + 1);
				if (salary.indexOf("-") > -1) {
					salaryMin = new BigDecimal(salary.substring(0, salary.indexOf("-")))
							.multiply(new BigDecimal("1000"));
					salaryMax = new BigDecimal(salary.substring(salary.indexOf("-") + 1, salary.indexOf("千")))
							.multiply(new BigDecimal("1000"));
				} else {
					salaryMin = new BigDecimal(salary.substring(0, salary.indexOf("千")))
							.multiply(new BigDecimal("1000"));
				}
			} else if (salary.indexOf("元") > -1) {
				salaryUnit = salary.substring(salary.indexOf("元") + 1);
				if (salary.indexOf("-") > -1) {
					salaryMin = new BigDecimal(salary.substring(0, salary.indexOf("-")));
					salaryMax = new BigDecimal(salary.substring(salary.indexOf("-") + 1, salary.indexOf("元")));
				} else {
					salaryMin = new BigDecimal(salary.substring(0, salary.indexOf("元")));
				}
			}
		}
		System.out.println("salaryMin:" + salaryMin + " salaryMax:" + salaryMax + " salaryUnit:" + salaryUnit);
		String companyName = doc.select(".cname").get(0).text();
		System.out.println("companyName:" + companyName);
		String companyType = doc.select(".ltype").get(0).text();
		System.out.println("companyType:" + companyType);
		List<String> jtag = doc.select(".jtag").get(0).select(".sp4").eachText();
		outputStringList("jtag", jtag);
		Integer expirenceMin = 0;
		Integer expirenceMax = 99;
		String education = "";
		Integer amount = 0;
		String date = "";
		if (jtag != null) {
			for (String tag : jtag) {
				if (tag.contains("经验")) {
					if ("无工作经验".equals(tag)) {
						continue;
					}
					if (tag.indexOf("-") > -1) {
						expirenceMin = new Integer(tag.substring(0, tag.indexOf("-")));
						expirenceMax = new Integer(tag.substring(tag.indexOf("-") + 1, tag.indexOf("年经验")));
					} else {
						expirenceMin = new Integer(tag.replaceAll("年", "").replaceAll("以上", "").replaceAll("经验", ""));
					}
				} else if (tag.contains("招")) {
					if (tag.contains("若干")) {
						amount = -1;
					} else {
						amount = new Integer(tag.replace("招", "").replace("人", ""));
					}
				} else if (tag.contains("发布")) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
					date = sdf.format(new Date()) + "-" + tag.replace("发布", "");
				} else if ("中技".equals(tag) || "初中及以下".equals(tag) || "初中".equals(tag) || "高中".equals(tag)
						|| "中专".equals(tag) || "大专".equals(tag) || "本科".equals(tag) || "硕士".equals(tag)
						|| "博士".equals(tag)) {
					education = tag;
				} else {
					System.err.println(tag);
				}
			}
		}
		System.out.println("expirenceMin:" + expirenceMin);
		System.out.println("expirenceMax:" + expirenceMax);
		System.out.println("education:" + education);
		System.out.println("amount:" + amount);
		System.out.println("date:" + date);
		List<String> jtag2 = null;
		if (doc.select(".jtag").get(0).select(".t2").size() > 0) {
			jtag2 = doc.select(".jtag").get(0).select(".t2").get(0).select("span").eachText();
		}
		String jtagString = outputStringList("jtag2", jtag2);
		// 2.jtag解析
		String jobMsg = doc.select(".job_msg").get(0).text();
		System.out.println("jobMsg:" + jobMsg);
		Elements fps = doc.select(".fp");
		List<String> keywords = null;
		String companyLocation = null;
		List<String> jobTypes = null;
		for (int i = 0; i < fps.size(); i++) {
			if ("关键字：".equals(fps.get(i).select(".label").get(0).text())) {
				keywords = fps.get(i).select(".el").eachText();
			}
			if ("上班地址：".equals(fps.get(i).select(".label").get(0).text())) {
				companyLocation = fps.get(i).text().replace("上班地址：", "");
			}
			if ("职能类型：".equals(fps.get(i).select(".label").get(0).text())) {
				jobTypes = fps.get(i).select(".el").eachText();
			}
		}
		outputStringList("keywords", keywords);
		outputStringList("jobTypes", jobTypes);
		System.out.println("companyLocation:" + companyLocation);
		String companyInfo = doc.select(".tmsg").get(0).text();
		System.out.println("companyInfo:" + companyInfo);
		// 创建一个文档对象
		SolrInputDocument sd = new SolrInputDocument();
		// 添加域
		sd.addField("url", url);
		sd.addField("jobName", jobName);
		sd.addField("location", location);
		sd.addField("salary", salary);
		sd.addField("salaryMin", salaryMin.toBigInteger());
		sd.addField("salaryMax", salaryMax.toBigInteger());
		sd.addField("salaryUnit", salaryUnit);
		sd.addField("companyName", companyName);
		sd.addField("companyType", companyType);
		sd.addField("expirenceMin", expirenceMin);
		sd.addField("expirenceMax", expirenceMax);
		sd.addField("education", education);
		sd.addField("amount", amount);
		sd.addField("date", date);
		sd.addField("jtag", jtagString);
		sd.addField("jobMsg", jobMsg);
		sd.addField("keywords", outputStringList("keywords", keywords));
		sd.addField("companyLocation", companyLocation);
		sd.addField("jobTypes", outputStringList("jobTypes", jobTypes));
		sd.addField("companyInfo", companyInfo);
		solrServer.add(sd);
		solrServer.commit();
		Thread.sleep(3000);
		// 只获取一遍,获取不到就丢弃
		// channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,
		// true);
	}

	private String outputStringList(String key, List<String> list) {
		String result = "";
		if (list != null) {
			for (String item : list) {
				result += item + " ";
				System.out.println(key + ":" + item);
			}
		}
		return result;
	}
}
